﻿//----------------------------------------------------------------
// Desc: 
// Edit: ShowLong, http://blog.csdn.net/showlong
//
// Date Created: 2010-06-11
//
// Update Date : 
//
// Copyright (c) Fuel4D (http://www.fuel4d.com), All rights reserved.
//
//----------------------------------------------------------------
#pragma once


FINLINE int FAtoiW(const WCHAR* str)
{
    if (!str)
        return 0;
#if FUEL_OS_WINDOWS
    return _wtoi(str);
#else
    wchar_t* pend;
    return (int)wcstol(str, &pend, 10);
#endif
}

FINLINE int FAtoiA(const ACHAR* str)
{
    if (!str)
        return 0;
    return atoi(str);
}

FINLINE float FAtofW(const WCHAR* str)
{
    if (!str)
        return 0;
#if FUEL_OS_WINDOWS
    return (float)_wtof(str);
#else
    wchar_t* pend;
    return (float)wcstod(str, &pend);
#endif
}

FINLINE float FAtofA(const ACHAR* str)
{
    if (!str)
        return 0;
    return (float)atof(str);
}

FINLINE double FAtodW(const WCHAR* str)
{
    if (!str)
        return 0;
#if FUEL_OS_WINDOWS
    return _wtof(str);
#else
    wchar_t* pend;
    return wcstod(str, &pend);
#endif
}

FINLINE double FAtodA(const ACHAR* str)
{
    if (!str)
        return 0;
    return atof(str);
}

FINLINE int FStrcmpW(const WCHAR* left, const WCHAR* right)
{
    if (left && right)
        return wcscmp(left, right);
    return left ? 1 : -1;
}

FINLINE int FStrcmpA(const ACHAR* left, const ACHAR* right)
{
    if (left && right)
        return strcmp(left, right);
    return left ? 1 : -1;
}

FINLINE int FStricmpW(const WCHAR* left, const WCHAR* right)
{
    if (left && right)
#if FUEL_OS_WINDOWS
        return _wcsicmp(left, right);
#else
        return FStrcasecmpW(left, right);
#endif
    return left ? 1 : -1;
}

FINLINE int FStricmpA(const ACHAR* left, const ACHAR* right)
{
    if (left && right)
#if FUEL_OS_WINDOWS
        return _stricmp(left, right);
#else
        return strcasecmp(left, right);
#endif
    return left ? 1 : -1;
}

FINLINE int FStrncmpW(const WCHAR* left, const WCHAR* right, size_t comp_count)
{
    if (left && right && comp_count)
        return wcsncmp(left, right, comp_count);
    if (comp_count <= 0)
        return 0;
    return left ? 1 : -1;
}

FINLINE int FStrncmpA(const ACHAR* left, const ACHAR* right, size_t comp_count)
{
    if (left && right && comp_count)
        return strncmp(left, right, comp_count);
    if (comp_count <= 0)
        return 0;
    return left ? 1 : -1;
}

FINLINE int FStrnicmpW(const WCHAR* left, const WCHAR* right, size_t comp_count)
{
    if (left && right && comp_count)
#if FUEL_OS_WINDOWS
        return _wcsnicmp(left, right, comp_count);
#else
        return FStrncasecmpW(left, right, comp_count);
#endif
    if (comp_count <= 0)
        return 0;
    return left ? 1 : -1;
}

FINLINE int FStrnicmpA(const ACHAR* left, const ACHAR* right, size_t comp_count)
{
    if (left && right)
#if FUEL_OS_WINDOWS
        return _strnicmp(left, right, comp_count);
#else
        return strncasecmp(left, right, comp_count);
#endif
    if (comp_count <= 0)
        return 0;
    return left ? 1 : -1;
}

FINLINE size_t FStrlenW(const WCHAR* s)
{
    if (!s)
        return 0;
    size_t n = 0;
    while (*s)
        s++,n++;
    return n;
}

FINLINE size_t FStrlenA(const ACHAR* s)
{
    if (s)
        return strlen(s);
    return 0;
}

FINLINE WCHAR FConvertCharW(WCHAR k, BYTE cov_type)
{
    if (cov_type == 1)
    {
        if (k >= _FTW('A') && k <= _FTW('Z'))
            return k - _FTW('A') + _FTW('a');
    }
    else if (cov_type == 2)
    {
        if (k >= _FTW('a') && k <= _FTW('z'))
            return k - _FTW('a') + _FTW('A');
    }
    return k;
}

FINLINE ACHAR FConvertCharA(ACHAR k, BYTE cov_type)
{
    if (cov_type == 1)
    {
        if (k >= _FTA('A') && k <= _FTA('Z'))
            return k - _FTA('A') + _FTA('a');
    }
    else if (cov_type == 2)
    {
        if (k >= _FTA('a') && k <= _FTA('z'))
            return k - _FTA('a') + _FTA('A');
    }
    return k;
}

template < size_t size >
FINLINE ACHAR* FStrcpyA(ACHAR (&dst)[size], const ACHAR* src, BYTE cov_type)
{
    return FStrcpyA_s(dst, src, size, cov_type);
}

template < size_t size >
FINLINE WCHAR* FStrcpyW(WCHAR (&dst)[size], const WCHAR* src, BYTE cov_type)
{
    return FStrcpyW_s(dst, src, size, cov_type);
}

template < size_t size >
FINLINE ACHAR* FStrcatA(ACHAR (&dst)[size], const ACHAR* src, BYTE cov_type)
{
    return FStrcatA_s(dst, src, size, cov_type);
}

template < size_t size >
FINLINE WCHAR* FStrcatW(WCHAR (&dst)[size], const WCHAR* src, BYTE cov_type)
{
    return FStrcatW_s(dst, src, size, cov_type);
}

FINLINE const WCHAR* FMemchrW(const WCHAR* src, WCHAR chr, size_t count)
{
    if (src && count)
        return wmemchr(src, chr, count);
    return 0;
}

FINLINE const ACHAR* FMemchrA(const ACHAR* src, ACHAR chr, size_t count)
{
    if (src && count)
        return (const ACHAR*)memchr(src, chr, count);
    return 0;
}

template < typename _Ty >
FINLINE _Ty FComboStringToTypeW(const WCHAR* cur_str, const _Ty* ty_list, const WCHAR* combo_str)
{
    FVStringW out;
    if (FComboStringW(out, combo_str))
    {
        for (size_t i = 0; i < out.size(); ++i)
        {
            if (FStrcmpW(out[i].c_str(), cur_str) == 0)
                return ty_list[i];
        }
    }
    return ty_list[0];
}

template < typename _Ty >
FINLINE _Ty FComboStringToTypeA(const ACHAR* cur_str, const _Ty* ty_list, const ACHAR* combo_str)
{
    FVStringA out;
    if (FComboStringA(out, combo_str))
    {
        for (size_t i = 0; i < out.size(); ++i)
        {
            if (FStrcmpA(out[i].c_str(), cur_str) == 0)
                return ty_list[i];
        }
    }
    return ty_list[0];
}

template < typename _Ty >
FINLINE const WCHAR* FComboStringToStrW(FStringW& ret, _Ty cur_str, const _Ty* ty_list, const WCHAR* combo_str)
{
    FVStringW out;
    if (FComboStringW(out, combo_str))
    {
        for (size_t i = 0; i < out.size(); ++i)
        {
            if (cur_str == ty_list[i])
            {
                ret = out[i];
                return ret.c_str();
            }
        }
    }
    ret = out[0];
    return ret.c_str();
}

template < typename _Ty >
FINLINE const ACHAR* FComboStringToStrA(FStringA& ret, _Ty cur_str, const _Ty* ty_list, const ACHAR* combo_str)
{
    FVStringA out;
    if (FComboStringA(out, combo_str))
    {
        for (size_t i = 0; i < out.size(); ++i)
        {
            if (cur_str == ty_list[i])
            {
                ret = out[i];
                return ret.c_str();
            }
        }
    }
    ret = out[0];
    return ret.c_str();
}
